<script setup>
import { inject, watchEffect, onUpdated } from 'vue';

const props = defineProps({
  position: Array,
});

const map = inject('map');
const AMap = inject('AMap');

let marker = null;
let inited = false;

watchEffect(() => {
  if (!map.value || !AMap.value) {
    return;
  }
  if (inited) {
    return;
  }
  marker = new AMap.value.Marker({
    position: new AMap.value.LngLat(...props.position),
  });
  // 将创建的点标记添加到已有的地图实例：
  map.value.add(marker);
  inited = true;
});

onUpdated(() => {
  marker.setPosition(new AMap.value.LngLat(...props.position));
});

</script>

<template></template>
